home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VideoControl / CPP / CPPVideoControl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  7.1 KB  |  256 lines

  1. //------------------------------------------------------------------------------
  2. // File: CPPVideoControl.cpp 
  3. //
  4. // Desc: Implementation of WinMain
  5. //       for the Windows XP MSVidCtl C++ sample
  6. //
  7. // Copyright (c) 2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10. // Note: Proxy/Stub Information
  11. //      To build a separate proxy/stub DLL, 
  12. //      run nmake -f CPPVideoControlps.mk in the project directory.
  13.  
  14. #include "stdafx.h"
  15. #include "resource.h"
  16. #include <commctrl.h>
  17.  
  18. #include "CPPVideoControl.h"
  19. #include "CPPVideoControl_i.c"
  20. #include "CompositeControl.h"
  21.  
  22. // Constants
  23. const DWORD dwTimeOut = 5000; // time for EXE to be idle before shutting down
  24. const DWORD dwPause = 2000;   // time to wait for threads to finish up
  25.  
  26. // Local prototypes
  27. BOOL CheckVersion(void);
  28.  
  29.  
  30. // Passed to CreateThread to monitor the shutdown event
  31. static DWORD WINAPI MonitorProc(void* pv)
  32. {
  33.     CExeModule* p = (CExeModule*)pv;
  34.     p->MonitorShutdown();
  35.     return 0;
  36. }
  37.  
  38. LONG CExeModule::Unlock()
  39. {
  40.     LONG lock = CComModule::Unlock();
  41.     if (lock == 0)
  42.     {
  43.         bActivity = true;
  44.         SetEvent(hEventShutdown); // tell monitor that we transitioned to zero
  45.     }
  46.     return lock;
  47. }
  48.  
  49. //Monitors the shutdown event
  50. void CExeModule::MonitorShutdown()
  51. {
  52.     DWORD dwWait=0;
  53.  
  54.     while (1)
  55.     {
  56.         WaitForSingleObject(hEventShutdown, INFINITE);
  57.  
  58.         do
  59.         {
  60.             bActivity = false;
  61.             dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut);
  62.  
  63.         } while (dwWait == WAIT_OBJECT_0);
  64.  
  65.         // timed out
  66.         if (!bActivity && m_nLockCnt == 0) // if no activity let's really quit
  67.         {
  68. #if _WIN32_WINNT >= 0x0400 && defined(_ATL_FREE_THREADED)
  69.             CoSuspendClassObjects();
  70.             if (!bActivity && m_nLockCnt == 0)
  71. #endif
  72.                 break;
  73.         }
  74.     }
  75.  
  76.     CloseHandle(hEventShutdown);
  77.     PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  78. }
  79.  
  80. bool CExeModule::StartMonitor()
  81. {
  82.     hEventShutdown = CreateEvent(NULL, false, false, NULL);
  83.     if (hEventShutdown == NULL)
  84.         return false;
  85.  
  86.     DWORD dwThreadID;
  87.     HANDLE h = CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
  88.     return (h != NULL);
  89. }
  90.  
  91. CExeModule _Module;
  92.  
  93. BEGIN_OBJECT_MAP(ObjectMap)
  94. OBJECT_ENTRY(CLSID_CompositeControl, CCompositeControl)
  95. END_OBJECT_MAP()
  96.  
  97.  
  98. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  99. {
  100.     while (p1 != NULL && *p1 != NULL)
  101.     {
  102.         LPCTSTR p = p2;
  103.         while (p != NULL && *p != NULL)
  104.         {
  105.             if (*p1 == *p)
  106.                 return CharNext(p1);
  107.             p = CharNext(p);
  108.         }
  109.         p1 = CharNext(p1);
  110.     }
  111.     return NULL;
  112. }
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. //
  116. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, 
  117.     HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  118. {
  119.     // This sample requires Windows XP.  If running on a downlevel system,
  120.     // exit gracefully.
  121.     if (!CheckVersion())
  122.         return FALSE;
  123.  
  124.     lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  125.  
  126. #if _WIN32_WINNT >= 0x0400 && defined(_ATL_FREE_THREADED)
  127.     HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  128. #else
  129.     HRESULT hRes = CoInitialize(NULL);
  130. #endif
  131.  
  132.     _ASSERTE(SUCCEEDED(hRes));
  133.     _Module.Init(ObjectMap, hInstance, &LIBID_CPPVIDEOCONTROLLib);
  134.     _Module.dwThreadID = GetCurrentThreadId();
  135.     TCHAR szTokens[] = _T("-/");
  136.  
  137.     int nRet = 0;
  138.     BOOL bRun = TRUE;
  139.     LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  140.     while (lpszToken != NULL)
  141.     {
  142.         if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  143.         {
  144.             _Module.UpdateRegistryFromResource(IDR_CPPVideoControl, FALSE);
  145.             nRet = _Module.UnregisterServer(TRUE);
  146.             bRun = FALSE;
  147.             break;
  148.         }
  149.         if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  150.         {
  151.             _Module.UpdateRegistryFromResource(IDR_CPPVideoControl, TRUE);
  152.             nRet = _Module.RegisterServer(TRUE);
  153.             bRun = FALSE;
  154.             break;
  155.         }
  156.         lpszToken = FindOneOf(lpszToken, szTokens);
  157.     }
  158.  
  159.     if (bRun)
  160.     {
  161.         _Module.StartMonitor();
  162. #if _WIN32_WINNT >= 0x0400 && defined(_ATL_FREE_THREADED)
  163.         hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
  164.                     REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
  165.         _ASSERTE(SUCCEEDED(hRes));
  166.         hRes = CoResumeClassObjects();
  167. #else
  168.         hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
  169.             REGCLS_MULTIPLEUSE);
  170. #endif
  171.         _ASSERTE(SUCCEEDED(hRes));
  172.         
  173.         //Create the window and have our composite control (with msvidctl) appear
  174.         AtlAxWinInit();
  175.         InitCommonControls();
  176.  
  177.         HWND hWnd = ::CreateWindow(TEXT("AtlAxWin"), 
  178.                     TEXT("CPPVideoControl.CompositeControl"),
  179.                     NULL,
  180.                     CW_USEDEFAULT, CW_USEDEFAULT,   // x,y
  181.                     300, 350,               // width, height
  182.                     NULL, NULL,             // parent, menu
  183.                     ::GetModuleHandle(NULL), 
  184.                     NULL);
  185.        _ASSERTE(hWnd != NULL);
  186.         hWnd = hWnd;    // Prevent C4189 (variable initialized but not used)
  187.  
  188.         MSG msg;
  189.         while (GetMessage(&msg, 0, 0, 0))
  190.         {
  191.             TranslateMessage( &msg );
  192.             DispatchMessage(&msg);
  193.         }
  194.  
  195.         _Module.RevokeClassObjects();
  196.         Sleep(dwPause); //wait for any threads to finish
  197.     }
  198.  
  199.     _Module.Term();
  200.     CoUninitialize();
  201.     return nRet;
  202. }
  203.  
  204. //
  205. //  called on a WM_CLOSE & WM_SYSCOMMAND (SC_CLOSE) message.  If appropriate, save settings
  206. //  and then destroy the window
  207. //
  208. LRESULT CCompositeControl::OnExit(WORD /*wNotifyCode*/, WORD /* wID */, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  209. {
  210.     SAFE_RELEASE(m_pATSCTuningSpace);
  211.     SAFE_RELEASE(m_pTuningSpaceContainer);
  212.     SAFE_RELEASE(m_pATSCLocator);
  213.     SAFE_RELEASE(m_pMSVidCtl);
  214.  
  215.     DestroyWindow();
  216.     PostQuitMessage(0);
  217.     return 0;
  218. }
  219.  
  220. //
  221. //  the user clicks on the system menu
  222. //
  223. LRESULT CCompositeControl::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  224. {
  225.     bHandled = FALSE; // always do default processing
  226.  
  227.     if (wParam == SC_CLOSE)
  228.         OnExit(NULL, NULL, NULL, bHandled);
  229.         
  230.     return 0;
  231. }
  232.  
  233.  
  234. BOOL CheckVersion( void ) 
  235. {
  236.     OSVERSIONINFO osverinfo={0};
  237.     osverinfo.dwOSVersionInfoSize = sizeof(osverinfo);
  238.  
  239.     if(GetVersionEx(&osverinfo)) {
  240.  
  241.         // Windows XP's major version is 5 and its' minor version is 1.
  242.         if((osverinfo.dwMajorVersion > 5) || 
  243.             ( (osverinfo.dwMajorVersion == 5) && (osverinfo.dwMinorVersion >= 1) )) {
  244.             return TRUE;
  245.         }
  246.     }
  247.  
  248.     MessageBox(NULL, 
  249.         TEXT("This application requires the Microsoft Video Control,\r\n")
  250.         TEXT("which is present only on Windows XP.\r\n\r\n")
  251.         TEXT("This sample will now exit."),
  252.         TEXT("Microsoft Video Control is required"), MB_OK);
  253.  
  254.     return FALSE;
  255. }
  256.